package gmed;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.io.*;
import java.sql.*;
import java.rmi.*;
import java.rmi.server.*;

public class Farmacia_Impl extends UnicastRemoteObject implements Farmacia {
	private int nlicenca;
	private String nome;
	private String morada;
	private int nif;
	private int idfarmacia;
	private static Connection conn = null;
	
	
	public Farmacia_Impl(int nlicenca) throws RemoteException{
		this.nlicenca=nlicenca;	
		this.nlicenca = nlicenca;
		if (conn == null)
			conn = DBConnection2.get_conn();
		java.sql.Statement st;
		try {
			st = conn.createStatement();
			java.sql.ResultSet rs = st
					.executeQuery("select nome,morada,nif, idfarmacia from farmacia where nlicenca="
							+ nlicenca);
			if (rs.first()) { 
				this.nome = rs.getNString(1);
				this.morada = rs.getNString(2);
				this.idfarmacia=rs.getInt(3);
				this.nif = rs.getInt(4);
			} else {
				if (st.execute("insert into farmacia (nlicenca) values ("
						+ nlicenca + ")", st.RETURN_GENERATED_KEYS)) {
					rs = st.getGeneratedKeys();
					rs.first();
					this.idfarmacia = rs.getInt(1);

				}
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	
	
	}
	public int get_nlicenca() throws RemoteException {
		return nlicenca;
	}
	public String get_nome() throws RemoteException {	
		return nome;
	}
	public String get_morada() throws RemoteException {
		return morada;
	}
	public int get_nif() throws RemoteException {
		return nif;
	}
	public boolean set_nlicenca(int nlicenca) throws RemoteException {
		this.nlicenca = nlicenca;
		this.update_field("nlicenca", nlicenca);
		return true;
	}
	public boolean set_nome(String nome) throws RemoteException {
		this.nome = nome;
		this.update_field("nome", nome);
		return true;
	}
	public boolean set_morada(String morada) throws RemoteException {
		this.morada = morada;
		this.update_field("morada", morada);
		return true;
	}
	public boolean set_nif(int nif) throws RemoteException {
		this.nif = nif;
		this.update_field("nif", nif);
		return true;
	}	
	private void update_field(String field, Object Value) {
		Statement st;
		try {
			if (conn == null)
				conn = DBConnection2.get_conn();
			st = conn.createStatement();
			String query = "update farmacia set " + field + "='"
					+ Value.toString() + "' where nlicenca=" + this.nlicenca;
			System.err.println(query);
			st.execute(query);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}
	public void elimina_farmacia(int nlicenca){
		antes_eliminar();
		Statement st;
		try{
			if(conn==null)
				conn= DBConnection2.get_conn();
			st=conn.createStatement();
			String query="delete from farmacia where nlicenca="+this.nlicenca;
			st.execute(query);
		}catch (SQLException e){
	}
	}
	private void antes_eliminar(){
		Statement  st;
		try{
			if(conn==null)
				conn=DBConnection2.get_conn();
			st=conn.createStatement();
			String query="delete from receitas where receitas.idfarmacia=(select idfarmacia from farmacia where nlicenca="+this.nlicenca+")";
			st.execute(query);
		}catch(SQLException e){
			
		}
	}
}

